/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable {

    //midlet
    private MobileMonstersMIDlet midlet;
    private Display display;
    //path
    private String pathShadow = "";
    private String pathZoroark = "";
    private String pathTitle = "";
    //path level
    //images        
    private Image imgTitle;
    //button
    //font
    public static Font SMALL_PLAIN_FONT;
    public static Font SMALL_BOLD_FONT;
    public static Font LARGE_BOLD_FONT;
    //canvas property
    private int CANVAS_WIDTH = getWidth();
    private int CANVAS_HEIGHT = getHeight();
    private int bgWidth = 0;
    private int warna;
    //positions
    private int x = 0;
    private int y = 0;
    private int xBg = 0;
    private int yBg = 0;
    private int xShadow = 0;
    private int yShadow = 0;
    private int xZoroark = 0;
    private int yZoroark = 0;
    private int jarakLevel = 0;
    private int xyArea[] = new int[4];
    //animated sprite
    //private AnimatedSprite spShadow;
    private Sprite spZoroark;
    private AnimatedSprite spBlueBird;
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    private static int FRAME_ZOROARK_WIDTH = 0;
    private static int FRAME_ZOROARK_HEIGHT = 0;
    private boolean running = false;
    private LayerManager lmgr;
    //speed 
    private int speedGame = 80;
    private int speedBg = 5;
    private int speedGeser = 50;
    
    public MenuCanvas(MobileMonstersMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        CANVAS_WIDTH = getWidth();
        CANVAS_HEIGHT = getHeight();

        jarakLevel = (int) (CANVAS_WIDTH * 0.9);
        //warna = 0xc6eeff;
        warna = 0xffffff;

        if (CANVAS_WIDTH < CANVAS_HEIGHT) {
            //portrait
            yBg = CANVAS_HEIGHT - 240;
        } else {
            //landscape
            yBg = 0;
        }

        FRAME_ZOROARK_WIDTH = 80;
        FRAME_ZOROARK_HEIGHT = 80;
        pathZoroark = "/pulifi.png";
        //pathZoroark = "/zoroark.png";

        if (CANVAS_WIDTH == 640) {
            FRAME_SHADOW_WIDTH = 61;
            FRAME_SHADOW_HEIGHT = 75;
            pathShadow = "/bluebird.png";

            pathTitle = "/titleNinjaRun640.png";
            bgWidth = 1280;

        } else {
            FRAME_SHADOW_WIDTH = 61;
            FRAME_SHADOW_HEIGHT = 75;
            pathShadow = "/bluebird.png";

            pathTitle = "/titleNinjaRun320.png";

            bgWidth = 640;

        }

        try {
            //bgScroll = new BackgroundScroll(Image.createImage(pathBackground));
            //bgScroll.setRefPixelPosition(xBg, yBg);
            //animated sprite
            //spShadow = new AnimatedSprite(Image.createImage(pathShadow),
            //        FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            //spShadow.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);

            //spZoroark = new AnimatedSprite(Image.createImage(pathZoroark),
            //        FRAME_ZOROARK_WIDTH, FRAME_ZOROARK_HEIGHT);

            spZoroark = new Sprite(Image.createImage(pathZoroark));
            //spZoroark.defineReferencePixel(FRAME_ZOROARK_WIDTH / 2, FRAME_ZOROARK_HEIGHT / 2);
            spZoroark.setTransform(Sprite.TRANS_ROT90);

            spBlueBird = new AnimatedSprite(Image.createImage(pathShadow),
                    FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            spBlueBird.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);

            lmgr = new LayerManager();
            lmgr.append(spBlueBird);
            lmgr.append(spZoroark);
            //lmgr.append(bgScroll);
        } catch (IOException ex) {
        }

        try {
            imgTitle = Image.createImage(pathTitle);


        } catch (IOException ex) {
        }

        SMALL_PLAIN_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        SMALL_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        LARGE_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);

        xShadow = (int) (CANVAS_WIDTH * 0.2);
        yShadow = (int) (CANVAS_HEIGHT * 0.8);

        xZoroark = (int) (CANVAS_WIDTH * 0.8);
        yZoroark = (int) (CANVAS_HEIGHT * 0.3);

        spBlueBird.setRefPixelPosition(xShadow, yShadow);
        spZoroark.setRefPixelPosition(xZoroark, yZoroark);

    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress
            checkForKeys();
            //checkForCollision();


            //System.out.println("xHelp : " + xHelp
            //        + " acuanMenu : " + acuanMenu);

            drawDisplay(g);

            //System.out.println(spShuriken.getX());
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    /*
    protected void sizeChanged(int i, int i1) {
    super.sizeChanged(i, i1);
    //System.out.println("Oit!");
    System.out.println(getWidth() + "x" +getHeight());
    }
     *
     */
    protected void drawDisplay(Graphics g) {

        g.setColor(warna);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (xBg == -bgWidth) {
            xBg = 0;
        } else {
            xBg = xBg - speedBg;
        }
        //bgScroll.setPosition(xBg, yBg);
        spBlueBird.nextFrame();
        spZoroark.nextFrame();
        lmgr.paint(g, 0, 0);

        g.drawImage(imgTitle, CANVAS_WIDTH / 2 - (imgTitle.getWidth() / 2), 0, Graphics.TOP | Graphics.LEFT);

        //paint disini
        //sampai sini        

        //flush off-screen buffer to display


        //g.setColor(0x0000ff);
        //g.drawString("for test only : " + getWidth() + "x" + getHeight(), 0, 10, Graphics.TOP | Graphics.LEFT);

        flushGraphics();
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & FIRE_PRESSED) != 0) {
            //isAttack = true;
            //keyPressed(-5);
            //System.out.println(getKeyStates());
        } else if ((keyState & LEFT_PRESSED) != 0) {
            //keyPressed(-3);
            //System.out.println(getKeyStates());
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            //keyPressed(-4);
            //System.out.println(getKeyStates());
        } else if ((keyState & UP_PRESSED) != 0) {
            //keyPressed(-1);
            //System.out.println(getKeyStates());
        } else if ((keyState & DOWN_PRESSED) != 0) {
            //keyPressed(-2);
            //System.out.println(getKeyStates());
        } else if ((keyState & GAME_A_PRESSED) != 0) {
            //System.out.println("A!!!");
        } else if ((keyState & GAME_B_PRESSED) != 0) {
            //System.out.println("B!!!");
        } else if ((keyState & GAME_C_PRESSED) != 0) {
            //System.out.println("C!!!");
        } else if ((keyState & GAME_D_PRESSED) != 0) {
            //System.out.println("D!!!");
        }
    }

    protected void keyPressed(int keyCode) {
        /*
        try {
            //System.out.println(keyCode);
            Thread.sleep(speedGame * 2);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
         * 
         */
        if (keyCode == -5) {
        } else if (keyCode == -2) { //down
        } else if (keyCode == -4) { //right
            System.out.println("menu : " + MobileMonstersMIDlet.CLICKED);
            if (MobileMonstersMIDlet.CLICKED < 1) {
                MobileMonstersMIDlet.CLICKED++;
                midlet.beforeShow(0);
            }
        } else if (keyCode == -1) { //up
        } else if (keyCode == -3) { //left
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
        }
        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;

        //show = false;


    }

    protected void pointerReleased(int x, int y) {
        //recognizer.endGesture(x, y);
        this.x = x;
        this.y = y;
        //show = true;

    }
}
